
package es.unex.sextante.libMath.simpleStats;

import java.util.ArrayList;

/**
 * A class to calculate some basic statistics
 * @author volaya
 *
 */
public class SimpleStats {

    private ArrayList m_Values;
    private double m_dSum;
    private double m_dRMS;
    private double m_dMean;
    private double m_dVariance;
    private double m_dMax, m_dMin;
    private double m_dStdDev;
    private boolean m_bCalculated;
    private int m_iCount;

    public SimpleStats() {

        m_Values = new ArrayList();
        m_bCalculated = false;
        m_iCount = 0;

    }

    /**
     * adds a new value to the list of values
     * @param dValue the value
     */
    public void addValue(double dValue) {

        m_Values.add(new Double(dValue));
        m_bCalculated = false;
        m_iCount++;

    }

    private void calculate() {

        int i;
        double dValue;

        if (!m_bCalculated) {
            m_dMax = Double.NEGATIVE_INFINITY;
            m_dMin = Double.MAX_VALUE;
            m_dSum = 0;
            m_dRMS = 0;
            for (i = 0; i < m_iCount; i++) {
                dValue = ((Double) m_Values.get(i)).doubleValue();
                m_dSum += dValue;
                m_dRMS += dValue * dValue;
                m_dMax = Math.max(m_dMax, dValue);
                m_dMin = Math.min(m_dMin, dValue);
            }
            m_dMean = m_dSum / (double) m_iCount;
            m_dVariance = m_dRMS / m_iCount - m_dMean * m_dMean;
            m_dStdDev = Math.sqrt(m_dVariance);
            m_dRMS = Math.sqrt(m_dRMS / m_iCount);

            m_bCalculated = true;

        }

    }

    /**
     * Returns the coefficient of variation
     * @return the coefficient of variation
     */
    public double getCoeffOfVar() {

        if (!m_bCalculated) {
            calculate();
        }

        return m_dVariance / m_dMean;

    }

    /**
     * Return the number of values added
     * @return the number of values added
     */
    public int getCount() {

        return m_iCount;

    }

    /**
     * Returns the maximum value
     * @return the maximum value
     */
    public double getMax() {

        if (!m_bCalculated) {
            calculate();
        }

        return m_dMax;

    }

    /**
     * Returns the mean value
     * @return the mean value
    **/
    public double getMean() {

        if (!m_bCalculated) {
            calculate();
        }

        return m_dMean;

    }
    /**
     * Returns the minimum value
     * @return the minimum value
    **/
    public double getMin() {

        if (!m_bCalculated) {
            calculate();
        }

        return m_dMin;

    }

    /**
     * Returns the Root Mean Squared
     * @return the Root Mean Squared
     */
    public double getRMS() {

        if (!m_bCalculated) {
            calculate();
        }

        return m_dRMS;

    }

    /**
     * Returns the standard deviation
     * @return the standard deviation
     */
    public double getStdDev() {

        return m_dStdDev;

    }

    /**
     * Returns the sum of all values
     * @return the sum of all values
     */
    public double getSum() {

        if (!m_bCalculated) {
            calculate();
        }

        return m_dSum;

    }

    /**
     * Returns the variance
     * @return the variance
     */
    public double getVariance() {

        if (!m_bCalculated) {
            calculate();
        }

        return m_dVariance;

    }

}
